#include "shuangren.h"
#include "ui_shuangren.h"
#include "QPainter"
#include"QTime"
#include"QMessageBox"
#include"QtGlobal"
QVector<QVector<int>> shuangren::chess;
shuangren* shuangren::pchessboard = nullptr;
shuangren *shuangren::getInstance()
{
    if(shuangren::pchessboard == nullptr)
    {
        shuangren::pchessboard = new shuangren();
    }
    return shuangren::pchessboard;
}
bool shuangren::winner()
{
    //横向扫描
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<11;j++)
        {
            if(chess[i][j]==1 && chess[i][j+1]==1 && chess[i][j+2]==1 && chess[i][j+3]==1 && chess[i][j+4]==1)
            {
                QMessageBox::information(nullptr,"温馨提示","玩家获胜!");
                this->newGame();
                return true;
            }
            else if(chess[i][j]==2 && chess[i][j+1]==2 && chess[i][j+2]==2 && chess[i][j+3]==2 && chess[i][j+4]==2)
            {
                QMessageBox::information(nullptr,"温馨提示","失败");
                this->newGame();
                return true;
            }
        }
    }

    //纵向扫描
    for(int j=0;j<15;j++)
    {
        for(int i=0;i<11;i++)
        {
            if(chess[i][j]==1 && chess[i+1][j]==1 && chess[i+2][j]==1 && chess[i+3][j]==1 && chess[i+4][j]==1)
            {
                QMessageBox::information(nullptr,"温馨提示","玩家获胜!");
                this->newGame();
                return true;
            }
            else if(chess[i][j]==2 && chess[i+1][j]==2 && chess[i+2][j]==2 && chess[i+3][j]==2 && chess[i+4][j]==2)
            {
                QMessageBox::information(nullptr,"温馨提示","失败!");
                this->newGame();
                return true;
            }
        }
    }

    //对角扫描
    for(int i=0;i<11;i++)
    {
        for(int j=0;j<11;j++)
        {
            if(chess[i][j]==1 && chess[i+1][j+1]==1 && chess[i+2][j+2]==1 && chess[i+3][j+3]==1 && chess[i+4][j+4]==1)
            {
                QMessageBox::information(nullptr,"温馨提示","玩家获胜!");
                this->newGame();
                return true;
            }
            else if(chess[i][j]==2 && chess[i+1][j+1]==2 && chess[i+2][j+2]==2 && chess[i+3][j+3]==2 && chess[i+4][j+4]==2)
            {
                QMessageBox::information(nullptr,"温馨提示","失败");
                this->newGame();
                return true;
            }
        }
    }

    //反对角扫描
    for(int j=0;j<11;j++)
    {
        for(int i=14;i>3;i--)
        {
            if(chess[i][j]==1 && chess[i-1][j+1]==1 && chess[i-2][j+2]==1 && chess[i-3][j+3]==1 && chess[i-4][j+4]==1)
            {
                QMessageBox::information(nullptr,"温馨提示","玩家获胜!");
                this->newGame();
                return true;
            }
            else if(chess[i][j]==2 && chess[i-1][j+1]==2 && chess[i-2][j+2]==2 && chess[i-3][j+3]==2 && chess[i-4][j+4]==2)
            {
                QMessageBox::information(nullptr,"温馨提示","失败");
                this->newGame();
                return true;
            }
        }
    }

    return false;
}

void shuangren::newGame()
{
    //初始化棋盘
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            chess[i][j] = 0;
        }
    }

    //释放棋盘窗口,包括棋子
    delete this->chessWin;

    //创建新棋盘窗口,并初始化
    this->chessWin = new QWidget(this);
    this->chessWin->resize(810,810);
    this->chessWin->show();
}
bool shuangren::piecePress(bool object, int x, int y)
{
    //动态创建标签插入棋子图，并初始化位置信息
    this->piece = new QLabel(this->chessWin);
    this->piece->setGeometry(x-x%54,y-y%54,54,54);
    this->piece->setScaledContents(true);
    //若下棋对象为玩家1
    if( object== player1)
    {
        //插入黑棋图
        this->piece->setPixmap(QPixmap(":/new/prefix1/picture/an.png"));
        //棋盘相应位置进行标记
        chess[x/54][y/54] = 1;
    }
    //若下棋对象为玩家2
    else if( object== player2)
    {
        //插入白棋图
        this->piece->setPixmap(QPixmap(":/new/prefix1/picture/guang.png"));
        //棋盘相应位置进行标记
        chess[x/54][y/54] = 2;
    }
    //棋子显示
    this->piece->show();
    //输赢判定，并返回判定结果
    return this->winner();
}
void shuangren::mousePressEvent(QMouseEvent *event)
{
    //若鼠标点击位置没有棋子，则玩家落子
    if(chess.at(event->x()/54).at(event->y()/54) == 0)
    {
        //返回值判断一方是否获胜
        bool result = this->piecePress(player1,event->x(),event->y());
        //玩家胜利时，跳过电脑落子重新开始游戏
        if(result == true)
        {
            return;
        }
        //直到生成随机数区域没有棋子，则电脑落子
        while(1)
        {
            //生成随机横坐标
            int computerx = qrand()%800;
            //生成随机纵坐标
            int computery = qrand()%800;
            //判断该位置是否有棋子
            if(chess.at(computerx/54).at(computery/54) == 0)
            {
                //电脑落子
                this->piecePress(player2,computerx,computery);
                break;
            }
        }
    }
}
shuangren::shuangren(QWidget *parent) : QWidget(parent)
{
    //隐藏标题栏
    this->setFixedSize(810,810);

    //创建棋盘背景
    this->bg = new QLabel(this);
    this->bg->setGeometry(0,0,810,810);
    this->bg->setPixmap(QPixmap(":/new/prefix1/picture/qipan.png"));
    this->bg->setScaledContents(true);

    //创建棋盘窗口
    this->chessWin = new QWidget(this);
    this->chessWin->resize(810,810);

    //初始化棋盘
    for(int i=0;i<15;i++)
    {
        QVector<int> row;
        for(int j=0;j<15;j++)
        {
            row.append(0);
        }
        chess.append(row);
    }

    //随机数初始化
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
}
shuangren::~shuangren()
{
    delete ui;
}
